//
//  ProductCellButton.m
//  jang
//
//  Created by lee han on 08/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "ProductCellButton.h"
#import "CRUserData.h"
#import "CommonView.h"

@implementation ProductCellButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (IBAction)onClickProduct:(id)sender {
    if(_mDelegate){
        [_mDelegate onClickProductCellButton:_mProductID];
    }
}
- (IBAction)onClickCollection:(id)sender {
    //发送收藏或取消收藏消息
    NSString* pid = [NSString stringWithFormat:@"%ld", _mProductID];
    
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 pid, @"pid",
                                 nil];
    
    if([[CRUserData sharedInstance] hasCollection:pid]){
        [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_CancenCollect];
    }
    else{
        [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_Collect];
    }
}

#pragma mark networkDelegate
- (void)parserResult:(NSDictionary *)response functionType:(NSInteger)type
{
    if([[response objectForKey:@"status"] integerValue] != 1){
        return;
    }
    
    if(type == NetworkFunctionType_Collect){
        [[CRUserData sharedInstance] addCollection:[NSString stringWithFormat:@"%ld", _mProductID]];
        [_mButton setTitle:@"取消" forState:UIControlStateNormal];
    }
    else if(type == NetworkFunctionType_CancenCollect){
        [[CRUserData sharedInstance] removeCollection:[NSString stringWithFormat:@"%ld", _mProductID]];
        [_mButton setTitle:@"收藏" forState:UIControlStateNormal];
        if(_mDelegate && [_mDelegate respondsToSelector:@selector(onDeleteCollection:)]){
            [_mDelegate onDeleteCollection:_mProductID];
        }
    }
}
//end networkDelegate
@end
